package com.twu.biblioteca.memberdata;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Maximilian
 * Date: 1/10/13
 * Time: 1:06 PM
 * To change this template use File | Settings | File Templates.
 */
public class MemberData {
    Map <String, Member> members = new HashMap<String, Member>();
    Set <Member> membersSet = new HashSet<Member>();
    IdManager idManager = new IdManager();

    public MemberData()
    {
    }

    public String addMember(String email, String password) throws OutOfMemberIdsException
    {
        Member member = new Member(idManager.getNextId(), email, password);
        members.put(member.getId(), member);
        membersSet.add(member);
        return member.getId();
    }

    public Collection<Member> getMembers() {
        return membersSet;
    }

    public Member getMemberByUsernamePassword(String id, String password) throws WrongUsernameOrPasswordException
    {
        if(!members.containsKey(id))
        {
            throw new WrongUsernameOrPasswordException("Wrong username or password");
        }
        Member member = members.get(id);
        if(!member.getPassword().equals(password))
        {
            throw new WrongUsernameOrPasswordException("Wrong username or password");
        }
        else
            return member;
    }
}
